package com.cnplan.service.impl;

import com.cnplan.core.KUtil;
import com.cnplan.core.PrintImage;
import com.cnplan.entity.ArcRoll;
import com.cnplan.mapper.ArcRollImageMapper;
import com.cnplan.mapper.ArcRollMapper;
import com.cnplan.service.ArcRollService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ArcRollServiceImpl implements ArcRollService {

    @Autowired
    private ArcRollMapper arcRollMapper;

    @Autowired
    private ArcRollImageMapper arcRollImageMapper;

    @Autowired
    private KUtil kUtil;

    private String url="";
    @Override
    public PageInfo findByArcRoll(Map map) {
        if(!map.containsKey("pageNo")){
            map.put("pageNo",1);
        }
        if(!map.containsKey("pageSize")){
            map.put("pageSize",10);
        }
        StringBuffer sb=new StringBuffer();
        String gName=map.containsKey("gName")?map.get("gName").toString():null;
        if(gName!=null){
            String[] gNameArray=gName.split(" ");
            for (String str:gNameArray) {
                sb.append(" and g_name like '%"+str.trim()+"%'");
            }
        }
        PageHelper.startPage(Integer.parseInt(map.get("pageNo").toString()),
                Integer.parseInt(map.get("pageSize").toString()));
        List<ArcRoll> List=arcRollMapper.selectArcRoll(map.get("rId").toString().trim(),sb+"");
//        rollImageDataSel(List);
        PageInfo pageInfo = new PageInfo(List);
        return pageInfo;
    }

    @Override
    public List<ArcRoll> findByArcRollList(Map map) {
        System.out.println("rId------------"+map.get("rId"));
//        if(!map.containsKey("pageNo")){
//            map.put("pageNo",1);
//        }
//        if(!map.containsKey("pageSize")){
//            map.put("pageSize",10);
//        }
        StringBuffer sb=new StringBuffer();
        String gName=map.containsKey("gName")?map.get("gName").toString():null;
        if(gName!=null){
            String[] gNameArray=gName.split(" ");
            for (String str:gNameArray) {
                sb.append(" and a.g_name like '%"+str.trim()+"%'");
            }
        }
        /*PageHelper.startPage(Integer.parseInt(map.get("pageNo").toString()),
                Integer.parseInt(map.get("pageSize").toString()));*/
        List<ArcRoll> List=arcRollMapper.selectArcRoll(map.get("rId").toString().trim(),sb+"");
//        TifStartThread tifStartThread=new TifStartThread(List);
//        tifStartThread.start();
        for (ArcRoll arcRoll:List) {
            arcRoll.setImageUrl(arcRoll.getImageUrl()==null || "".equals(arcRoll.getImageUrl())?null:arcRoll.getImageUrl().replace("\\","/"));
        }
        return List;
    }

    @Override
    public ArcRoll findById(String Id) {
        return arcRollMapper.findById(Id);
    }

    @Override
    public PageInfo findByArcRollData(Map map) {
        if(!map.containsKey("pageNo")){
            map.put("pageNo",1);
        }
        if(!map.containsKey("pageSize")){
            map.put("pageSize",10);
        }
        StringBuffer sb=new StringBuffer();
        String gName=map.containsKey("gName")?map.get("gName").toString():null;
        if(gName!=null){
            String[] gNameArray=gName.split(" ");
            for (String str:gNameArray) {
                sb.append(" and g_name like '%"+str.trim()+"%'");
            }
        }
        PageHelper.startPage(Integer.parseInt(map.get("pageNo").toString()),
                Integer.parseInt(map.get("pageSize").toString()));
        List<ArcRoll> List=arcRollMapper.selectArcRollData(map.get("arcNum").toString().trim(),sb+"");
        PageInfo pageInfo = new PageInfo(List);
        return pageInfo;
    }

    @Override
    public void batchPrinting(Map map) {
        String listStr= (String) map.get("ids");
        String[] ids=listStr.split(",");
        for (int i=0;i<ids.length;i++) {
            String id= ids[i];
            if(id!=null && !"".equals(id)){
                ArcRoll arcRoll=arcRollMapper.findArcRollId(id);
                int imgNum=Integer.parseInt(arcRoll.getImageNum());
                for (int j=0;j<imgNum;j++) {
                    PrintImage.drawImage(url+arcRoll.getImageUrl()+"/"+j+".jpg");
                }
            }
        }
    }

}
